/**
 * 料号生效地控制
 */

package ext.tianma.part.validators;

import java.util.Locale;

import wt.fc.Persistable;
import wt.fc.WTReference;
import wt.lifecycle.State;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

public class UpdateEffectPartNumberValidators extends DefaultUIComponentValidator{
	//新建物料
	private static final String NEW_PAER = "NewPart";
	//新建物料修改
	private static final String NEW_PART_MODIFY = "NewPartModify";
	@Override
	public UIValidationResultSet performFullPreValidation( UIValidationKey uivalidationKey , UIValidationCriteria uivalidationCriteria, Locale locale ) throws WTException {
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		boolean flag = false;
		
		if (uivalidationCriteria != null && uivalidationCriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationCriteria.getContextObject();
			if(null == wtreference){
				uivresultSet.addResult( new UIValidationResult(uivalidationKey, uivalidationstatus ) );
				return uivresultSet;
			}
			Persistable persistable = wtreference.getObject();

			WTPart part = null;			
			if(persistable instanceof WTPart)
			{			
				part = (WTPart) persistable;
			}
			
			if( part != null ){
				State partState = part.getLifeCycleState();
				
				if( partState != null ){
					String state = partState.toString();
					//修改：“新建物料修改”状态下也打开“料号修改”入口。原因：流程驳回时物料状态为“新建物料修改”。
					if( state.equals(NEW_PAER) || state.equals( NEW_PART_MODIFY ) ){
						flag = true ;
					}
				}
			}
		
		}
		if (flag) {
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.ENABLED));
		} else {
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.HIDDEN));
		}
		
		return uivresultSet;
	}
}
